/*
    Developer: John Archer (john.archer.jpa@gmail.com)
 
    This file is part of VTBuilder.

    VTBuilder is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License.

    VTBuilder is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with VTBuilder.  If not, see <http://www.gnu.org/licenses/>.
 */

package core.tasks.pack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class RPartPack extends Thread{
    public ArrayList <String> pOut = new ArrayList <String> ();
    private final CountDownLatch stopLatch;
    private List <String> pIn;
    private HashMap<String, Integer> lib;
    public RPartPack(CountDownLatch stopLatch, List <String> pIn, HashMap<String, Integer>  wm){
        this.stopLatch = stopLatch;
        this.pIn = pIn;
        this.lib = wm;
    }

    public void run(){
        partition();
        stopLatch.countDown();
    }
    //used in read partitioning - very quick heuristic here
    //im not looking in a single window at end
    //im looking for any window
    //so indels in a single window not so important as a diff window can be used if they exist
    private void partition(){
        //int df;
        boolean [] sHt;
        int [] sdf;
        HashMap <Integer, Integer> hsh = new HashMap<Integer, Integer>(250);      
        int mD;int mF;int hts;int swin;int ewin;int freq;int counter = 0;  
        for(String s : pIn) {
            mD=0;mF=0;
            sdf = new int[s.length()-10];
            hsh.clear(); 
            for (int k=0; k < sdf.length; k++) {
                if (lib.get(s.substring(k, k + 10))!=null) {
                    sdf[k] =  lib.get(s.substring(k, k + 10)) - k;
                    if(hsh.get(sdf[k])==null){hsh.put(sdf[k], 1);}
                    else{
                        freq = hsh.get(sdf[k])+1;
                        if(freq>mF){mF=freq;mD=sdf[k];}
                        hsh.put(sdf[k], freq);
                    }
                }
            }
            if(mF > 30){
                sHt = new boolean[sdf.length];
                for (int c = 0; c < sdf.length; c++){sHt[c] = (sdf[c]==mD);}
                hts = 0;
                for(int x=0; x<100; x++){if(sHt[x]){hts++;}}
                if(++hts>69){
                    pOut.add(s);
                    pIn.set(counter, null);
                }
                else{
                    swin = 1;ewin = 101;
                    inner: while(ewin<sHt.length){
                        if(sHt[ewin] && !sHt[swin]){
                            if(++hts>69){
                                pOut.add(s); 
                                pIn.set(counter, null);
                                break inner;
                            }
                        }
                        else if(!sHt[ewin] && sHt[swin]){hts--;}
                        ewin++;swin++;
                    }
                }
            }
            counter++;
        }
    }  
}